/**
 * 时间 ： 2014-5-27 上午11:20:53
 * 作者 ： HaiTao
 * 描述 ： 当前类的描述信息
 **/
package com.laitaoo.service.home
{
	import com.laitaoo.event.HomeEvent;
	import com.laitaoo.service.BaseService;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncResponder;

	[RouteEvents(events="productPriceResult,treeYearResult")]
	[Event(name="treeYearResult", type="com.laitaoo.event.HomeEvent")]
	[Component]
	public class HomeService extends BaseService
	{
		public function HomeService()
		{
			super();
		}
		
		public function getTreeData():void{
			
			var sql:String = "SELECT * FROM years";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(loginResultHandler,loginFaultHandler,token));
		}
		private function loginResultHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows() as ArrayCollection;
			
			// 先组织一个三维数组 [{label:2014,mouth:[{label:5,days:[{label:27},{label:28}]},{label:6}]}]
			var treeData:XML = <xml label='年度列表'></xml>;
			
			getYears(rows,treeData);//计算年份
			getMouths(rows,treeData); // 计算月份
			getDays(rows,treeData); // 计算日期
			
			// 显示年份
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.TREE_YEAR_RESULT);
			homeEvent.years = treeData;
			dispatchEvent(homeEvent);
		}
		/* 计算年份*/
		private function getYears(rows:ArrayCollection,treeData:XML):void{
			
			for(var i:int = 0;i<rows.length;i++){
				
				var rowObj:Object = rows.getItemAt(i);
				
				if(validatorYears(treeData,rowObj.year))continue;
				
				var yearXml:XML = new XML("<year id='"+rowObj.Id+"' label='"+rowObj.year+"'></year>");
				
				treeData.appendChild(yearXml);
			}
		}
		// 验证是否有重复的年份
		private function validatorYears(treeData:XML,year:String):Boolean{
			
			for each (var xml:XML in treeData.children()) 
			{
				if(xml.@label.toString() == year){
					return true;
				}
			}
			return false;
		}
		// 获取年份中的月份		
		private function getMouths(rows:ArrayCollection,treeData:XML):void{
			
			for(var i:int = 0;i<rows.length;i++){
				
				var rowObj:Object = rows.getItemAt(i);
			
				for each (var yearXml:XML in treeData.children()) 
				{
					if(rowObj.year == yearXml.@label.toString()){
						
						if(validatorYears(yearXml,rowObj.mouth))continue;
						
						var mouthXml:XML = new XML("<mouth id='"+rowObj.Id+"' label='"+rowObj.mouth+"'></mouth>");
						
						yearXml.appendChild(mouthXml);
					}
				}
			}
		}
		// 获取月份中的年份
		private function getDays(rows:ArrayCollection,treeData:XML):void{
			
			for(var i:int = 0;i<rows.length;i++){
				
				var rowObj:Object = rows.getItemAt(i);
				
				for each (var yearXml:XML in treeData.children()) 
				{
					if(rowObj.year == yearXml.@label.toString()){
						
						var mouths:XMLList = yearXml.children();
						
						for each (var mouthXml:XML in mouths) 
						{
							if(mouthXml.@label == rowObj.mouth){
								
								var dayXml:XML = new XML("<day id='"+rowObj.Id+"' label='"+rowObj.day+"'></day>");
								
								mouthXml.appendChild(dayXml);
							}
						}
					}
				}
			}
		}
		private function loginFaultHandler(info:Object, token:Object):void{
		}
		
		//-----------------------------------------------------------------
		// 商品单价
		//------------------------------------------------------------------
		
		/**
		 * 获取指定商品的单价
		 * 
		 * @param productId:String --- 指定商品ID
		 * */
		public function getProductPrice(productId:String):void{
			
			var sql:String = "SELECT * FROM storage where productId='"+productId+"'";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(productPriceHandler,productPriceFaultHandler,token));
		}
		
		private function productPriceHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows() as ArrayCollection;
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.productPriceResult);
			homeEvent.storageObj = rows.getItemAt(0);
			dispatchEvent(homeEvent);
		}
		private function productPriceFaultHandler(info:Object, token:Object):void{
		}
		
		
		
	}
}